import { DynamicInstanceConfig, GlobalHelp, ModelService } from '@ibiz/dynamic-model-api';
import { Component, Host, h, Prop, ComponentInterface } from '@stencil/core';
import { ModelLocationController } from '../../controllers';
import { GlobalControllers } from '../../global';

@Component({
  tag: 'os-main',
  styleUrl: 'os-main.scss',
})
export class OsMain implements ComponentInterface {
  @Prop()
  ctrl: ModelLocationController = GlobalControllers.modelLocal;

  async componentDidLoad(): Promise<void> {
    GlobalHelp.install(ModelService, async (path: string, _config: DynamicInstanceConfig) => {
      try {
        const res = await fetch(`http://l.zhr.icu:10001${path}`, { mode: 'cors' });
        const data = await res.json();
        return data;
      } catch (err) {
        console.log(err);
      }
      return null;
    });
    const view = await ModelService.getPSAppView('PSSYSAPPS/DynaModelViewer/PSAPPDEVIEWS/PSAppFuncDataView.json');
    await this.ctrl.open(view, {}, {});
  }

  render() {
    return <Host></Host>;
  }
}
